//
//  MineViewController.m
//  JackRadar
//
//  Created by Pei on 15/11/9.
//  Copyright © 2015年 Xiaobo Pei. All rights reserved.
//

#import "MineViewController.h"
#import "HTHorizontalSelectionList.h"
//#import "ZXMultiFormatWriter.h"
#import "UIView+Additions.h"
#import "ZXImage.h"
#import "SettingViewController.h"
#import "UserInfo.h"
#import "UIImageView+WebCache.h"

@interface MineViewController ()<HTHorizontalSelectionListDelegate, HTHorizontalSelectionListDataSource>
{
    UIScrollView *_mainScrollView;
    UIView *_topView;
    HTHorizontalSelectionList *_segementView;
    NSArray *_titleArray;
    
    UIImageView *_QRImageView;
    
    UIImageView *_headerImageView;
    UILabel *_nameLbl;
    UILabel *_numLbl;
    UILabel *_contentLbl1;
    UILabel *_contentLbl2;
    
    NSDictionary *_currentDic;
    
    NSString *_currentAction;
    //ZXMultiFormatWriter *_writer;
    //ZXBitMatrix *_result;
}

@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Not_Hide_TabBar_Notification object:nil];
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] postNotificationName:Hide_TabBar_Notification object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _titleArray = @[@"我的二维码", @"项目二维码"];
    
    _mainScrollView = [[UIScrollView alloc] init];
    _mainScrollView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-kTabHeight);
    [self.view addSubview:_mainScrollView];
    
    [self initTopView];
    [self initSelectView];
    [self initQRCode];
    
    [self getData];
}

#pragma mark *************************** UI *********************************
- (void)initTopView
{
    _topView = [[UIView alloc] init];
    _topView.frame = CGRectMake(0.0f, 0.0f, kDeviceWidth, (kDeviceHeight-kTabHeight)/2);
    [_mainScrollView addSubview:_topView];
    
    UIImageView *backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02beijing-"]];
    backImageView.frame = CGRectMake(0.0f, 0.0f, _topView.frame.size.width, _topView.frame.size.height);
    [_topView addSubview:backImageView];
    
    _headerImageView = [[UIImageView alloc] init];
    _headerImageView.image = [UIImage imageNamed:@""];
    _headerImageView.backgroundColor = kWhiteColor;
    _headerImageView.layer.masksToBounds = YES;
    _headerImageView.layer.cornerRadius = 5.0f;
    _headerImageView.frame = CGRectMake(16.0f, 40.0f, 80, 80);
    [_topView addSubview:_headerImageView];
    
    _nameLbl = [[UILabel alloc] init];
    _nameLbl.frame = CGRectMake(_headerImageView.right+10.0f, _headerImageView.origin.y, kDeviceWidth-16*2.0f-_headerImageView.width-60.0f-10.0f*2, Home_Small_Size+2);
    _nameLbl.textColor = kWhiteColor;
    _nameLbl.font = kAppFont(Home_Small_Size+2);
    _nameLbl.text = @"姓名：";
    [_topView addSubview:_nameLbl];
    
    _numLbl = [[UILabel alloc] init];
    _numLbl.frame = CGRectMake(_headerImageView.right+10.0f, _nameLbl.bottom+8.0f, kDeviceWidth-16*2.0f-_headerImageView.width-60.0f-10.0f*2, Home_Small_Size+2);
    _numLbl.textColor = kWhiteColor;
    _numLbl.font = kAppFont(Home_Small_Size+2);
    _numLbl.text = @"编号：";
    [_topView addSubview:_numLbl];
    
    //84 105 202
    UIButton *updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    updateBtn.frame = CGRectMake(_headerImageView.right+10.0f, _headerImageView.bottom-35.0f, 90.0f, 35.0f);
    [updateBtn setBackgroundColor:kRGB(84, 105, 202)];
    [updateBtn setTitle:@"同步信息" forState:UIControlStateNormal];
    updateBtn.layer.masksToBounds = YES;
    updateBtn.layer.cornerRadius = 4.0f;
    updateBtn.titleLabel.font = kAppFont(16.0f);
    [updateBtn addTarget:self action:@selector(updateBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_topView addSubview:updateBtn];
    
    UIControl *settingControl = [[UIControl alloc] init];
    settingControl.frame = CGRectMake(kDeviceWidth-16.0f-60.0f, _headerImageView.origin.y, 60.0f, 20.0f);
    [settingControl addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside];
    [_topView addSubview:settingControl];
    
    UIImageView *setttingImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"01shezhi"]];
    setttingImageView.frame = CGRectMake(0.0f, 0.0f, 20.0f, 20.0f);
    [settingControl addSubview:setttingImageView];
    
    UILabel *settingLbl = [[UILabel alloc] init];
    settingLbl.text = @"设置";
    settingLbl.textColor = kWhiteColor;
    settingLbl.frame = CGRectMake(setttingImageView.right, (settingControl.height-(Home_Small_Size+2.0f))/2.0f, 40.0f, Home_Small_Size+2.0f);
    settingLbl.font = kAppFont(Home_Small_Size+2.0f);
    settingLbl.textAlignment = NSTextAlignmentRight;
    [settingControl addSubview:settingLbl];
    
    UIView *topBottomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, _headerImageView.bottom+10.0f, kDeviceWidth, _topView.height-_headerImageView.bottom-10.0f)];
    topBottomView.backgroundColor = kRGBA(0, 0, 0, 0.16f);
    [_topView addSubview:topBottomView];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(16.0f, topBottomView.height/2.0f, kDeviceWidth-16.0f*2.0f, 0.5f)];
    lineView.backgroundColor = kWhiteColor;
    [topBottomView addSubview:lineView];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"03suoshibumen"]];
    imageView1.frame = CGRectMake(16.0f, (topBottomView.height/2.0f-(Home_Small_Size+2.0f+8.0f+Home_Small_Size))/2.0f, Home_Small_Size+6.0f, Home_Small_Size+2.0f);
    [topBottomView addSubview:imageView1];
    
    UILabel *titleLbl1 = [[UILabel alloc] initWithFrame:CGRectMake(imageView1.right+8.0f, imageView1.origin.y, 80, Home_Small_Size+2.0f)];
    titleLbl1.textColor = kWhiteColor;
    titleLbl1.text = @"所属部门";
    titleLbl1.font = kAppFont(Home_Small_Size+2.0f);
    [topBottomView addSubview:titleLbl1];
    
    _contentLbl1 = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, imageView1.bottom+8.0f, kDeviceWidth-16.0f, Home_Small_Size)];
    _contentLbl1.textColor = kWhiteColor;
    _contentLbl1.font = kAppFont(Home_Small_Size);
    _contentLbl1.text = @"";
    [topBottomView addSubview:_contentLbl1];

    UIImageView *imageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"04suoshuxiangmu-"]];
    imageView2.frame = CGRectMake(16.0f, topBottomView.height/2.0f+(topBottomView.height/2.0f-(Home_Small_Size+2.0f+8.0f+Home_Small_Size))/2.0f, Home_Small_Size+4.0f, Home_Small_Size+2.0f);
    [topBottomView addSubview:imageView2];
    
    UILabel *titleLbl2 = [[UILabel alloc] initWithFrame:CGRectMake(imageView2.right+10.0f, imageView2.origin.y, 80, Home_Small_Size+2.0f)];
    titleLbl2.textColor = kWhiteColor;
    titleLbl2.text = @"所属项目";
    titleLbl2.font = kAppFont(Home_Small_Size+2.0f);
    [topBottomView addSubview:titleLbl2];
    
    _contentLbl2 = [[UILabel alloc] initWithFrame:CGRectMake(16.0f, imageView2.bottom+8.0f, kDeviceWidth-16.0f, Home_Small_Size)];
    _contentLbl2.textColor = kWhiteColor;
    _contentLbl2.font = kAppFont(Home_Small_Size);
    _contentLbl2.text = @"";
    [topBottomView addSubview:_contentLbl2];

}

- (void)initSelectView
{
    _segementView = [[HTHorizontalSelectionList alloc] initWithFrame:CGRectMake(0.0f, _topView.frame.size.height, kDeviceWidth, 40.0f)];
    _segementView.delegate = self;
    _segementView.dataSource = self;
    _segementView.autoFillWidth = YES;
    _segementView.font = 14;
    _segementView.sideMargin = 0;
    _segementView.isAdjustBottomLine = YES;
    _segementView.bottomLineMargin = 10.f;
    _segementView.selectionIndicatorColor = kBlueColor;
    [_segementView setSelectedButtonIndex:0];
    [_segementView setTitleColor:kGreenColor forState:UIControlStateNormal];
    [_segementView setBottomTrimColor:kGreenColor];
    [_mainScrollView addSubview:_segementView];
}

- (void)initQRCode
{
    UIView *bottomView = [[UIView alloc] init];
    bottomView.frame = CGRectMake(0.0f, _segementView.bottom, kDeviceWidth, kDeviceHeight-kTabHeight-_topView.height-_segementView.height);
    [_mainScrollView addSubview:bottomView];
    
    _QRImageView = [[UIImageView alloc] init];
    _QRImageView.frame = CGRectMake((kDeviceWidth-(bottomView.height-20.0f))/2.0f, 10.0f, bottomView.height-20.0f, bottomView.height-20.0f);
    [bottomView addSubview:_QRImageView];
    
    //_writer = [[ZXMultiFormatWriter alloc] init];
    
}

#pragma mark ******************************* Action *************************************
- (void)settingAction
{
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    [self.navigationController pushViewController:settingVC animated:YES];
}

- (void)updateBtnAction
{
    //[[YMNetLib getInstance] connectWithAction:Dictionary_Type_API method:@"GET" withContent:[NSString stringWithFormat:@"DicType=%@", @"InspectionState"] withBody:nil withDelegate:self];
    //_currentAction = Dictionary_Type_API;
    
    
    [[YMNetLib getInstance] connectWithAction:Get_UserInfo_API method:@"GET" withContent:[NSString stringWithFormat:@"token=%@", [[UserInfo getInstance] getUseToken]] withBody:nil withDelegate:self];
    _currentAction = @"Update_Get_UserInfo_API";
}

#pragma mark ********************************** Data **********************************************
- (void)getData
{
    [[YMNetLib getInstance] connectWithAction:Get_UserInfo_API method:@"GET" withContent:[NSString stringWithFormat:@"token=%@", [[UserInfo getInstance] getUseToken]] withBody:nil withDelegate:self];
    _currentAction = Get_UserInfo_API;
}

#pragma mark ********************************* delagate ******************************************
- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentAction isEqualToString:Get_UserInfo_API])
    {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
        {
            _currentDic = body;
            
            [_headerImageView sd_setImageWithURL:[body valueForKey:@"UserImageUrl"] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
            _nameLbl.text = [NSString stringWithFormat:@"姓名：%@", [body valueForKey:@"UserName"]];
            _numLbl.text = [NSString stringWithFormat:@"编号：%@", [body valueForKey:@"OrgCode"]];
            _contentLbl1.text = [body valueForKey:@"OrgFullName"];
            _contentLbl2.text = [body valueForKey:@"ProjectName"];
            
            /*
             _result = [_writer encode:[body valueForKey:@"UserQRCodeUrl"]
             format:kBarcodeFormatQRCode
             width:_QRImageView.width
             height:_QRImageView.width
             error:nil];
             
             if (_result) {
             ZXImage *image = [ZXImage imageWithMatrix:_result];
             _QRImageView.image = [UIImage imageWithCGImage:image.cgimage];
             } else {
             _QRImageView.image = nil;
             }
             */
            [_QRImageView sd_setImageWithURL:[body valueForKey:@"UserQRCodeUrl"] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
        }
        else
        {
            if ([[body objectForKey:@"error"] intValue] == 4120)
            {
                //[[YMUIUtil getInstance] showTipViewAutoDis:@"密码错误"];
            }
            
        }
    }
    if([_currentAction isEqualToString:@"Update_Get_UserInfo_API"]){
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            [[UserInfo getInstance] setMineUserInfo:body];
           [[YMUIUtil getInstance] showTipViewAutoDis:@"同步成功"];
        }else{
            [[YMUIUtil getInstance] showTipViewAutoDis:@"同步失败"];
        }
    }
    if ([_currentAction isEqualToString:Dictionary_Type_API])
    {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
        {
            [[NSUserDefaults standardUserDefaults] setObject:body forKey:Mine_Update_UserInfo];
        }
        else
        {
        }
    }
    
}


#pragma mark - HTHorizontalSelectionList delegate
- (NSInteger)numberOfItemsInSelectionList:(HTHorizontalSelectionList *)selectionList
{
    return _titleArray.count;
}

- (NSString *)selectionList:(HTHorizontalSelectionList *)selectionList titleForItemWithIndex:(NSInteger)index
{
    return _titleArray[index];
}

- (void)selectionList:(HTHorizontalSelectionList *)selectionList didSelectButtonWithIndex:(NSInteger)index
{
    NSLog(@"========");
    
    switch (index) {
        case 0:
        {
            [_QRImageView sd_setImageWithURL:[_currentDic valueForKey:@"UserQRCodeUrl"] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
        }
            break;
        case 1:
        {
            [_QRImageView sd_setImageWithURL:[_currentDic valueForKey:@"ProjectQRCodeUrl"] placeholderImage:[UIImage imageNamed:@"normalDefault"]];
        }
            break;
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
